home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / appshell / appopsv.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-09-06  |  14.3 KB  |  473 lines

  1. VERSION 2.00
  2. Begin Form AppOpenSave 
  3.    BorderStyle     =   3  'Fixed Double
  4.    Caption         =   "File Open"
  5.    ClientHeight    =   3150
  6.    ClientLeft      =   1080
  7.    ClientTop       =   2895
  8.    ClientWidth     =   4980
  9.    Height          =   3555
  10.    Icon            =   APPOPSV.FRX:0000
  11.    Left            =   1020
  12.    LinkMode        =   1  'Source
  13.    LinkTopic       =   "Form1"
  14.    MaxButton       =   0   'False
  15.    MinButton       =   0   'False
  16.    ScaleHeight     =   3150
  17.    ScaleWidth      =   4980
  18.    Top             =   2550
  19.    Width           =   5100
  20.    Begin ListBox DirectoriesList 
  21.       Height          =   1785
  22.       Left            =   1950
  23.       Sorted          =   -1  'True
  24.       TabIndex        =   1
  25.       Top             =   1170
  26.       Width           =   1575
  27.    End
  28.    Begin FileListBox FilesList 
  29.       Height          =   1785
  30.       Left            =   165
  31.       TabIndex        =   3
  32.       Top             =   1170
  33.       Width           =   1575
  34.    End
  35.    Begin CommandButton CancelCmd 
  36.       Cancel          =   -1  'True
  37.       Caption         =   "&Cancel"
  38.       Height          =   375
  39.       Left            =   3705
  40.       TabIndex        =   7
  41.       Top             =   615
  42.       Width           =   1095
  43.    End
  44.    Begin CommandButton OKCmd 
  45.       Caption         =   "&OK"
  46.       Default         =   -1  'True
  47.       Height          =   375
  48.       Left            =   3705
  49.       TabIndex        =   6
  50.       Top             =   135
  51.       Width           =   1095
  52.    End
  53.    Begin TextBox FileName 
  54.       Height          =   315
  55.       Left            =   1140
  56.       TabIndex        =   5
  57.       Text            =   "FileName"
  58.       Top             =   165
  59.       Width           =   2400
  60.    End
  61.    Begin Label DirectoriesLbl 
  62.       Caption         =   "&Directories:"
  63.       Height          =   195
  64.       Left            =   1950
  65.       TabIndex        =   0
  66.       Top             =   900
  67.       Width           =   1035
  68.    End
  69.    Begin Label FilesLbl 
  70.       AutoSize        =   -1  'True
  71.       Caption         =   "&Files:"
  72.       Height          =   195
  73.       Left            =   150
  74.       TabIndex        =   2
  75.       Top             =   915
  76.       Width           =   465
  77.    End
  78.    Begin Label Directory 
  79.       Height          =   255
  80.       Left            =   1155
  81.       TabIndex        =   9
  82.       Top             =   580
  83.       Width           =   2310
  84.    End
  85.    Begin Label DirectoryLbl 
  86.       Caption         =   "Directory:"
  87.       Height          =   255
  88.       Left            =   170
  89.       TabIndex        =   8
  90.       Top             =   580
  91.       Width           =   855
  92.    End
  93.    Begin Label FileNameLbl 
  94.       Caption         =   "File &Name:"
  95.       Height          =   255
  96.       Left            =   170
  97.       TabIndex        =   4
  98.       Top             =   210
  99.       Width           =   975
  100.    End
  101. DefInt A-Z
  102. Dim CurFocus        As Integer
  103. Dim UpdateDirList   As Integer
  104. Dim SetFileLimit    As Integer
  105. Dim LastChange      As Integer
  106. Dim SaveDuplicate   As Integer
  107. Dim App_OpenSaveTitle As String
  108. Function BuildSpec (fpath As String) As String
  109.   If Right$(fpath, 1) <> "\" Then
  110.     s$ = fpath + "\*.*"
  111.   Else
  112.     s$ = fpath + "*.*"
  113.   End If
  114.   BuildSpec = s$
  115. End Function
  116. Sub CancelCmd_Click ()
  117.   App_DialogReturn = IDCANCEL
  118.   AppOpenSave.Hide
  119. End Sub
  120. Function CheckFileQual (fqual As String) As Integer
  121.   CheckFileQual = True
  122.   x% = InStr(fqual, ".")
  123.   If x% = 0 Or x% > 9 Or (Len(fqual) - x%) > 3 Then
  124.     CheckFileQual = False
  125.   Else
  126.     If InStr(x% + 1, fqual, ".") <> 0 Then CheckFileQual = False  ' too many .
  127.   End If
  128.   If InStr(fqual, ",") <> 0 Or InStr(fqual, "/") <> 0 Or InStr(fqual, " ") <> 0 Then
  129.     CheckFileQual = False
  130.   End If
  131. End Function
  132. Sub DirectoriesList_Click ()
  133.   Dim pqual As String
  134.   Dim fqual As String
  135.   a$ = FileName.Text
  136.   SplitFileName a$, pqual, fqual
  137.   If App_OpenSaveStyle = APP_OPEN Then
  138.     If InStr(fqual, "*") = 0 And InStr(fqual, "?") = 0 Then
  139.       temp$ = "*" + App_FileExtension
  140.     Else
  141.       temp$ = fqual
  142.     End If
  143.   Else
  144.     temp$ = ""
  145.   End If
  146.   sel$ = DirectoriesList.Text
  147.   If Mid$(sel$, 2, 1) = "-" Then
  148.     FileName.Text = Mid$(sel$, 3, 1) + ":" + temp$
  149.   Else
  150.     FileName.Text = Mid$(sel$, 2, Len(sel$) - 2) + "\" + temp$
  151.   End If
  152. End Sub
  153. Sub DirectoriesList_Dblclick ()
  154.   OKCmd_Click
  155. End Sub
  156. Sub DirectoriesList_GotFocus ()
  157.   If UpdateDirList Then
  158.     '
  159.     ' first time load of directories list
  160.     '
  161.     CurFocus = GetFocus()
  162.     x& = SendMessage(CurFocus, LB_DIR, &HC010, ByVal BuildSpec((FilesList.Path)))
  163.     UpdateDirList = False
  164.     FileName.SelStart = 0
  165.     FileName.SelLength = Len(FileName.Text)
  166.     If Visible Then FileName.SetFocus
  167.   End If
  168. End Sub
  169. Sub DirectoriesList_KeyPress (KeyAscii As Integer)
  170.   ' check if the user presses the Return key while on a valid entry
  171.   If KeyAscii = 13 Then
  172.     If DirectoriesList.ListIndex > -1 Then
  173.       DirectoriesList_Dblclick
  174.     End If
  175.   End If
  176. End Sub
  177. Sub FileName_Change ()
  178.   LastChange = 1
  179.   If FileName.Text = "" Then
  180.     OKCmd.Enabled = False
  181.   Else
  182.     OKCmd.Enabled = True
  183.   End If
  184. End Sub
  185. Sub FileName_GotFocus ()
  186.   If SetFileLimit Then
  187.     '
  188.     ' limit the text entry to not more than 127 characters
  189.     '
  190.     i% = 127
  191.     TextLimit& = SendMessage(GetFocus(), EM_LIMITTEXT, i%, ByVal "")
  192.     SetFileLimit = False
  193.   End If
  194. End Sub
  195. Sub FileName_KeyPress (KeyAscii As Integer)
  196.   If KeyAscii = 13 Then KeyAscii = 0   ' get rid of the beep
  197.   If KeyAscii = 0 Then
  198.     temp$ = RemoveSpaces((FileName.Text))
  199.     If App_OpenSaveStyle = APP_SAVE Then
  200.       '
  201.       ' do save checking here - add extension if user forgot
  202.       '
  203.       If InStr(temp$, ".") = 0 And Right$(temp$, 1) <> "\" And Right$(temp$, 1) <> ":" Then
  204.     temp$ = temp$ + App_FileExtension
  205.       End If
  206.       If Validate_SaveFileName(temp$, fout$) Then
  207.     App_FileName = fout$
  208.     LastChange = 5
  209.     OKCmd_Click
  210.       End If
  211.     Else
  212.       '
  213.       ' open file checking
  214.       '
  215.       If Validate_OpenFileName(temp$, fout$) Then
  216.     App_FileName = fout$
  217.     LastChange = 5
  218.     OKCmd_Click
  219.       End If
  220.     End If
  221.   End If
  222. End Sub
  223. Sub FilesList_Click ()
  224.   If App_OpenSaveStyle = APP_OPEN Then
  225.     a$ = FilesList.FileName
  226.     If InStr(a$, ".") = 0 Then a$ = a$ + "."
  227.     FileName.Text = a$
  228.     LastChange = 2
  229.   End If
  230. End Sub
  231. Sub FilesList_DblClick ()
  232.   If App_OpenSaveStyle = APP_OPEN Then
  233.     LastChange = 2
  234.     OKCmd_Click
  235.   Else
  236.     '
  237.     ' duplicate file name on save
  238.     '
  239.     SaveDuplicate = True
  240.     If UCase$(FilesList.FileName) <> UCase$(App_FileName) Then
  241.       x% = MsgBox("Replace Existing " + UCase$(FilesList.FileName) + "?", MB_ICONEXCLAMATION + MB_YESNO + MB_DEFBUTTON2, APP_NAME)
  242.       If x% = IDNO Then
  243.     Debug.Print "do not save the file!"
  244.     SaveDuplicate = False
  245.       End If
  246.     End If
  247.   End If
  248. End Sub
  249. Sub FilesList_KeyPress (KeyAscii As Integer)
  250.   If KeyAscii = 13 Then
  251.     If FilesList.ListIndex > -1 Then FilesList_DblClick
  252.   End If
  253. End Sub
  254. Sub Form_GotFocus ()
  255.   If UpdateDirList Then
  256.     FilesList.SetFocus          ' Set the Focus to fill the ListBox
  257.   End If
  258. End Sub
  259. Sub Form_Load ()
  260.   Screen.MousePointer = HOURGLASS
  261.   Remove_Items_From_SysMenu AppOpenSave
  262.   Place_DialogBox_in_Form AppOpenSave, AppMain
  263.   UpdateDirList = True     ' Update Drive/Subdirectory listbox first time
  264.   SetFileLimit = True      ' Limit text length of file name first time
  265.   ' load dialog box caption
  266.   If App_OpenSaveStyle = APP_OPEN Then
  267.     If App_OpenTitle = "" Then
  268.       AppOpenSave.Caption = "File Open"
  269.     Else
  270.       AppOpenSave.Caption = App_OpenTitle
  271.     End If
  272.   Else
  273.     If App_SaveTitle = "" Then
  274.       AppOpenSave.Caption = "File Save As"
  275.     Else
  276.       AppOpenSave.Caption = App_SaveTitle
  277.     End If
  278.   End If
  279.   App_OpenSaveTitle = AppOpenSave.Caption
  280.   ' load the path
  281.   If App_Path <> "" Then
  282.     If Right$(App_Path, 1) = "\" Then
  283.       App_Path = Left$(App_Path, (Len(App_Path) - 1))
  284.       If Right$(App_Path, 1) = ":" Then App_Path = App_Path + "\"
  285.     End If
  286.     FilesList.Path = App_Path
  287.   Else
  288.     App_Path = FilesList.Path
  289.   End If
  290.   ' set the pattern
  291.   If App_FileExtension <> "" Then
  292.     FilesList.Pattern = "*" + App_FileExtension
  293.   End If
  294.   ' load default file name
  295.   If App_OpenSaveStyle = APP_OPEN Then
  296.     FileName.Text = FilesList.Pattern
  297.   Else
  298.     FileName.Text = App_FileName
  299.   End If
  300.   App_Drive = Left$(FilesList.Path, 2)
  301.   Directory.Caption = FilesList.Path
  302.   ' adjust the dialog's controls for open or save
  303.   If App_OpenSaveStyle = APP_OPEN Then
  304.     FilesLbl.Visible = True
  305.     FilesLbl.Left = 165
  306.     FilesList.Visible = True
  307.     FilesList.Left = 165
  308.     DirectoriesList.Left = 1950
  309.     DirectoriesLbl.Left = 1950
  310.   Else
  311.     FilesLbl.Visible = False
  312.     FilesList.Visible = False
  313.     DirectoriesList.Left = 165
  314.     DirectoriesLbl.Left = 165
  315.   End If
  316.   Screen.MousePointer = DEFAULT
  317. End Sub
  318. Sub OKCmd_Click ()
  319.   Select Case LastChange
  320.     Case 1
  321.       FileName_KeyPress (13)
  322.     Case 2
  323.       App_Path = FilesList.Path
  324.       App_FileName = FilesList.FileName
  325.       If InStr(App_FileName, ".") = 0 Then App_FileName = App_FileName + "."
  326.       App_FullFileName = Left$(BuildSpec((FilesList.Path)), Len(BuildSpec((FilesList.Path))) - 3) + FilesList.FileName
  327.       App_DialogReturn = IDOK
  328.       AppOpenSave.Hide
  329.     Case 3
  330.       DirectoriesList_Dblclick
  331.     Case 4
  332.       AppOpenSave.Hide
  333.     Case 5
  334.       App_Path = FilesList.Path
  335.       App_FullFileName = Left$(BuildSpec((FilesList.Path)), Len(BuildSpec((FilesList.Path))) - 3) + App_FileName
  336.       App_DialogReturn = IDOK
  337.       AppOpenSave.Hide
  338.     Case Else
  339.     End Select
  340. End Sub
  341. Sub RefreshDirectoriesList ()
  342.       
  343.   Directory.Caption = FilesList.Path
  344.   DirectoriesList.SetFocus
  345.   CurFocus = GetFocus()
  346.   Y& = SendMessage(CurFocus, LB_RESETCONTENT, 0, ByVal "")
  347.   x& = SendMessage(CurFocus, LB_DIR, &HC010, ByVal BuildSpec((FilesList.Path)))
  348.   ChDir FilesList.Path
  349. End Sub
  350. Function RemoveSpaces (TheText$) As String
  351.   t$ = TheText$
  352.   i% = 1
  353.     i% = InStr(i%, t$, " ")
  354.     If i% = 0 Then Exit Do
  355.     t$ = Left$(t$, i% - 1) + Mid$(t$, i% + 1)
  356.   Loop
  357.   RemoveSpaces = t$
  358. End Function
  359. Function Validate_OpenFileName (fn As String, fqual As String) As Integer
  360.   Dim temp As String
  361.   Dim pqual As String
  362.   Dim OldPath As String
  363.   Dim FileNotFound As Integer
  364.   Dim FirstCheck As Integer
  365.   On Error GoTo OpenFileNameError
  366.   Validate_OpenFileName = False
  367.   OldPath = FilesList.Path
  368.   SplitFileName fn, pqual, fqual
  369.   ' first assign any path change entered
  370.   If pqual <> "" Then FilesList.Path = pqual
  371.   ' now check the the file name
  372.   ' if it exists already a dblclick will be generated in
  373.   ' the files list control otherwise a file error will be
  374.   ' generated (i.e. file not found, bad file name )
  375.   FileNotFound = False
  376.   FirstCheck = True
  377.   FilesList.FileName = "*."
  378.   FilesList.FileName = fqual
  379.   If FileNotFound Then
  380.     FirstCheck = False
  381.     FilesList.FileName = "*."
  382.     fqual = fqual + App_FileExtension
  383.     FilesList.FileName = fqual
  384.   Else
  385.     If AppOpenSave.Visible Then
  386.       '
  387.       ' must have been a directory or pattern change only so update dir. list
  388.       '
  389.       RefreshDirectoriesList
  390.       If InStr(fqual, "*") = 0 And InStr(fqual, "?") = 0 Then
  391.     FilesList.Pattern = "*" + App_FileExtension
  392.       End If
  393.       FileName.Text = FilesList.Pattern
  394.       FileName.SelStart = 0
  395.       FileName.SelLength = 0
  396.       FileName.SetFocus
  397.     End If
  398.   End If
  399. Exit_OpenFunction:
  400.   On Error GoTo 0
  401.   Exit Function
  402. OpenFileNameError:
  403.   f$ = FilesList.Path
  404.   If Right$(f$, 1) <> "\" Then f$ = f$ + "\"
  405.   f$ = f$ + fqual
  406.   Select Case Err
  407.     Case FILE_NOT_FOUND
  408.       If FirstCheck And InStr(fqual, ".") = 0 Then
  409.     FileNotFound = True
  410.     Resume Next
  411.       End If
  412.       e$ = "Cannot find " + UCase$(f$) + CRLF + CRLF + "check to ensure that the path and file are correct."
  413.     Case PATH_NOT_FOUND
  414.       e$ = "Directory does not exist;" + CRLF + CRLF + "check to ensure you specified the correct directory."
  415.     Case Else
  416.       e$ = Error$(Err) + ";" + CRLF + CRLF + "check to ensure the filename has no more than 8 characters followed by a period and a 3-letter extension." + CRLF + CRLF + "No spaces, commas, or backslashes (/) are allowed."
  417.   End Select
  418.   FilesList.Path = OldPath
  419.   FilesList.Pattern = "*" + App_FileExtension
  420.   Beep
  421.   MsgBox e$, MB_ICONEXCLAMATION, APP_NAME
  422.   Resume Exit_OpenFunction
  423. End Function
  424. Function Validate_SaveFileName (fn As String, fqual As String) As Integer
  425.   Dim temp As String
  426.   Dim pqual As String
  427.   Dim OldPath As String
  428.   On Error GoTo SaveFileNameError
  429.   Validate_SaveFileName = False
  430.   OldPath = FilesList.Path
  431.   SplitFileName fn, pqual, fqual
  432.   ' first assign any path change entered
  433.   If pqual <> "" Then FilesList.Path = pqual
  434.   ' now check the the file name portion
  435.   If fqual <> "" Then
  436.     If Not CheckFileQual(fqual) Then Error 64
  437.     '
  438.     ' if it exists already a dblclick will be generated in
  439.     ' the files list control otherwise a file error will be
  440.     ' generated (i.e. file not found, bad file name )
  441.     '
  442.     SaveDuplicate = False
  443.     FilesList.FileName = "*."
  444.     FilesList.FileName = fqual
  445.     If SaveDuplicate Then Validate_SaveFileName = True
  446.   Else
  447.     '
  448.     ' directory change
  449.     '
  450.     RefreshDirectoriesList
  451.     FileName.Text = ""
  452.     FileName.SetFocus
  453.   End If
  454. Exit_SaveFunction:
  455.   On Error GoTo 0
  456.   Exit Function
  457. SaveFileNameError:
  458.   If Err <> FILE_NOT_FOUND Then
  459.     Beep
  460.     Select Case Err
  461.       Case PATH_NOT_FOUND
  462.     e$ = "Directory does not exist;" + CRLF + CRLF + "check to ensure you specified the correct directory."
  463.       Case Else
  464.     e$ = Error$(Err) + ";" + CRLF + CRLF + "check to ensure the filename has no more than 8 characters followed by a period and a 3-letter extension." + CRLF + CRLF + "No spaces, commas, or backslashes (/) are allowed."
  465.     End Select
  466.     MsgBox e$, MB_ICONEXCLAMATION, APP_NAME
  467.     FilesList.Path = OldPath
  468.   Else
  469.     Validate_SaveFileName = True
  470.   End If
  471.   Resume Exit_SaveFunction
  472. End Function
  473.